/**
 * 
 */
package com.qdairlines.controller.i18n;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.qdairlines.common.ControllerUtil;
import com.qdairlines.common.Page;
import com.qdairlines.common.PageUtil;
import com.qdairlines.config.MessageResource;
import com.qdairlines.constant.BaseConsts;
import com.qdairlines.constant.RequestConsts;
import com.qdairlines.controller.base.BaseController;
import com.qdairlines.entity.i18n.Resource;
import com.qdairlines.service.i18n.MessageResourceService;

/**
 * @author pangyannan
 * @date 2017年6月9日
 * @company QingDao Airlines
 * @description 国际化控制器
 */
@Controller
@RequestMapping("i18n")
public class I18nController extends BaseController{
	@Autowired
	private MessageResourceService messageResourceService;
	@Autowired
	private MessageResource messageSource;

	/**
	 * 进入文件管理页面
	 * @return
	 */
    @RequestMapping( value = "toListPage" ,produces =RequestConsts.PRODUCES)
    public String toListPage(HttpServletRequest request,Model model) {
    	model.addAttribute(BaseConsts.LANGUAGE, ControllerUtil.getLanguage(request));
    	return "/i18n/messageResourcelist";
    }	

    /**
     * 文件清单数据Page
     * @param request
     * @param dictionary
     * @return
     */
    @RequestMapping(value = "pageMessageResource",produces = RequestConsts.PRODUCES)
    @ResponseBody
    public String pageMessageResource(HttpServletRequest request,Resource resource) {
    	if(StringUtils.isEmpty(resource.getText())){
    		resource.setText(null);
    	}
    	if(StringUtils.isEmpty(resource.getName())){
    		resource.setName(null);
    	}
    	if(StringUtils.isEmpty(resource.getLanguage())){
    		resource.setLanguage(null);
    	}
    	Page page = messageResourceService.listData(resource, PageUtil.getPageFromRequest(request));
    	return ControllerUtil.getJson(page);
    }
    
    @RequestMapping(value = "delete",produces = RequestConsts.PRODUCES)
    @ResponseBody
    public String delete(@RequestParam Integer resourceId) {
    	messageResourceService.deleteByPrimaryKey(resourceId);
    	messageSource.reload();
    	return ControllerUtil.getJson(BaseConsts.RESPONSE_MAP_SUCCESS);
    }
    
    @RequestMapping(value = "save",produces = RequestConsts.PRODUCES)
    @ResponseBody
    public String save(Resource resource) {
    	resource.setResourceId(null);
    	messageResourceService.saveSelective(resource);
    	messageSource.reload();
    	return ControllerUtil.getJson(BaseConsts.RESPONSE_MAP_SUCCESS);
    }
    
    @RequestMapping(value = "update",produces = RequestConsts.PRODUCES)
    @ResponseBody
    public String update(Resource resource) {
    	messageResourceService.updateByPrimaryKeySelective(resource);
    	messageSource.reload();
    	return ControllerUtil.getJson(BaseConsts.RESPONSE_MAP_SUCCESS);
    }
    
}
